package com.demo.Main.FtpUtil.WtDemo;

import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.demo.Main.FtpUtil.FTPTools;

import java.io.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo1 {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                5,
                1,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>());
        executor.execute(() -> {
            try (FileInputStream fis = new FileInputStream("localfile/info.json");
            ) {
                String str = new String(fis.readAllBytes());
                JSONArray objects = new JSONArray(str);
                int size = objects.size();
                for (int i = 0; i < size; i++) {
                    JSONObject json = objects.getJSONObject(i);
                    System.out.println(objects.get(0));
                    try {
                        boolean upload = FTPTools.upload(json.getStr("ip"),
                                json.getInt("port"),
                                json.getStr("username"),
                                json.getStr("password"),
                                "",
                                new FileInputStream(json.getStr("file")),
                                json.getStr("savefile"));
                        System.out.println(upload?json.getStr("ip") + "文件发送成功":json.getStr("ip") + "文件发送失败");
                    } catch (FileNotFoundException e) {
                        System.out.println("文件发送失败");
                    }
                }
            } catch (FileNotFoundException e) {
                System.out.println("文件发送失败");
            } catch (IOException e) {
                System.out.println("文件发送失败");
            }
        });
        executor.shutdown();
    }
}

